package com.lungle.warmdiary.app.view.active.adapter;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.response.Active;
import com.lungle.warmdiary.app.view.active.ActiveTab;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 主页活动列表数据适配器
 */
public class ActiveAdapter extends ListBaseAdapter {
    public static final int TYPE_COMMON = 0x00;
    public static final int TYPE_NOSTATUS = 0x01;
    private int type;

    private final long TIME_HARF_HOUR = 30;

    public ActiveAdapter(int type) {
        this.type = type;
    }

    @Override
    protected View getRealView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_active, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        final Active item = (Active) mDatas.get(position);
        vh.title.setText(item.getName());


        SpannableString ss = new SpannableString(String.format("本活动由%s创建", item.getAdvertiser()));
        ss.setSpan(new ForegroundColorSpan(Color.GRAY), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.BLACK), 4, 4 + item.getAdvertiser().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.GRAY), 4 + item.getAdvertiser().length(), ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        vh.publisher.setText(ss);
        //显示状态
        if (item.getStatus() == 0) {
            vh.countdown.setText("待确认");
            vh.countdown.setTextColor(0xFF897172);
        } else {
            ActiveTab activeTab = ActiveTab.getTabByIdx(item.getStatus());
            vh.countdown.setText(activeTab.getTitle());
            vh.countdown.setTextColor(activeTab.getColor());
        }
        Resources res = convertView.getContext().getResources();
        Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.ic_wechat_friend);

        vh.receiver.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(res, bitmap), null, null, null);
        vh.receiver.setPadding(4, 0, 2, 0);
        vh.receiver.setText(item.getMeida());

        List<String> itemImages = item.getImages();
        if (itemImages.size() > 5) {
            itemImages = itemImages.subList(0, 5);
        }


        vh.gridview.setClickable(false);
        vh.gridview.setEnabled(false);
        vh.gridview.setPressed(false);

        vh.gridview.setAdapter(new ActiveImageAdapter(convertView.getContext(), itemImages));

        if (type == TYPE_NOSTATUS) {
            vh.bonus.setVisibility(View.INVISIBLE);
            vh.countdown.setVisibility(View.INVISIBLE);
            vh.countdownTip.setVisibility(View.INVISIBLE);
            vh.rewardTip.setVisibility(View.INVISIBLE);
        } else {
            vh.bonus.setText(item.getBudget() + "");

        }


        return convertView;
    }


//    class ImageAdapter extends BaseAdapter {
//
//        private List<String> list;
//        private Context c;
//
//        public ImageAdapter(Context context, List<String> list) {
//            this.list = list;
//            this.c = context;
//        }
//
//        @Override
//        public int getCount() {
//            return list.size();
//        }
//
//        @Override
//        public Object getItem(int position) {
//            return position;
//        }
//
//        @Override
//        public long getItemId(int position) {
//            return position;
//        }
//
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            if (convertView == null) {
//                convertView = getLayoutInflater(c).inflate(
//                        R.layout.item_active_picture, parent, false);
//            }
//            ImageView picture = BaseViewHolder.get(convertView, R.id.iv_active_img);
//            ImageLoader.getInstance().displayImage(list.get(position), picture);
//            return convertView;
//        }
//    }


    static class ViewHolder {


        @Bind(R.id.tv_active_title)
        TextView title;
        @Bind(R.id.tv_active_bonus)
        TextView bonus;
        @Bind(R.id.tv_active_publisher)
        TextView publisher;
        //        @Bind(R.id.iv_active_channel)
//        ImageView ivActiveChannel;
        @Bind(R.id.tv_active_receiver)
        TextView receiver;
        @Bind(R.id.tv_active_countdown)
        TextView countdown;

        @Bind(R.id.tv_active_countdown_tip)
        TextView countdownTip;

        @Bind(R.id.iv_reward_tip)
        ImageView rewardTip;

        @Bind(R.id.gv_active)
        GridView gridview;

        public ViewHolder(View view) {

            ButterKnife.bind(this, view);
        }
    }
}
